package com.hy.study.algorithm.prim;

import java.util.Arrays;

/**
 * Prim 算法
 */
public class PrimAlgorithm {
    public static void main(String[] args) {
//测试图是否创建成功
        char[] data = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int verxs = data.length;
        //邻接矩阵的关系使用数组表示
        int[][] weight = new int[][]{
                //10000表示两个数不联通
                {10000, 5, 7, 10000, 10000, 10000, 2},
                {5, 10000, 10000, 9, 10000, 10000, 3},
                {7, 10000, 10000, 10000, 8, 10000, 10000},
                {10000, 9, 10000, 10000, 10000, 4, 10000},
                {10000, 10000, 8, 10000, 10000, 5, 4},
                {10000, 10000, 10000, 4, 5, 10000, 6},
                {2, 3, 10000, 10000, 4, 6, 10000}
        };

        //创建顶点
        MGraph mGraph = new MGraph(verxs);
        MinTree minTree = new MinTree();
        minTree.createGraph(mGraph, verxs, data, weight);
        minTree.showGraph(mGraph);
        minTree.prim(mGraph,1);
    }


}

/**
 * 创建一个最小生成树 --> 村庄的图
 */
class MinTree {
    /**
     * @param graph  图对象
     * @param verxs  图对象的顶点个数
     * @param data   图的各个顶点值
     * @param weight 图的邻接矩阵
     */
    public void createGraph(MGraph graph, int verxs, char data[], int[][] weight) {
        int i, j;
        for (i = 0; i < verxs; i++) {
            graph.data[i] = data[i];
            for (j = 0; j < verxs; j++) {
                graph.weight[i][j] = weight[i][j];
            }
        }
    }

    /**
     * 显示图
     */
    public void showGraph(MGraph graph) {
        for (int[] link : graph.weight) {
            System.out.println(Arrays.toString(link));
        }
    }

    /**
     * 得到最小生成树
     *
     * @param graph 图
     * @param v     表示从图的第几个顶点开始 'A'--'0' 'B'--'1'....
     */
    public void prim(MGraph graph, int v) {
        //表示节点是否被访问过
        int visited[] = new int[graph.verxs];
        //将当前节点标志为已访问
        visited[v] = 1;
        //h1 h2 用来记录两个顶点的下标
        int h1 = -1;
        int h2 = -1;
        int minWeight = 10000;//将minweight 标志为10000，后面遍历时会被更改
        for (int k = 1; k < graph.verxs; k++) {//生成 graph.verxs-1 条边
            //   这个是确定每一次生成的子图，和那个节点的距离距离最近
            for (int i = 0; i < graph.verxs; i++) {//遍历已经访问过的节点
                for (int j = 0; j < graph.verxs; j++) {//遍历还未访问过的节点
                    if (visited[i] == 1 && visited[j] == 0 && graph.weight[i][j] < minWeight) {
                        minWeight = graph.weight[i][j];
                        h1 = i;
                        h2 = j;
                    }
                }
            }
            //找到了一条最小边
            System.out.println("边<"+graph.data[h1]+","+graph.data[h2]+">权值:"+minWeight);
            //将当前节点标志为已访问
            visited[h2]=1;
            //将minweight重新设置为最大值
            minWeight=10000;
        }
    }
}


class MGraph {
    int verxs;//表示图的节点个数
    char[] data;//存放节点数据
    int[][] weight;//存放边，就是我们的邻接矩阵

    public MGraph(int verxs) {
        this.verxs = verxs;
        data = new char[this.verxs];
        weight = new int[this.verxs][this.verxs];
    }
}
